package com.guodi.map.utils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Base64;

/**
 * @author pwm
 * @date 2022/5/26
 */
public class Base64Util {

    /**
     * base64字符串转化成图片
     * @param imgStr imgStr
     * @return
     */
    public static InputStream generateImage(String imgStr) {
        try {
            Base64.Decoder decoder = Base64.getDecoder();
            // 去掉base64前缀 data:image/jpeg;base64,
            imgStr = imgStr.substring(imgStr.indexOf(",", 1) + 1);
            byte[] b = decoder.decode(imgStr);
            // 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            return new ByteArrayInputStream(b);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}